package com.wennimei.service;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.wennimei.dao.ParameterDAO;
import com.wennimei.domain.Parameters;
import com.wennimei.enums.MessageTypeEnum;
import com.wennimei.util.HttpUtil;
import com.wennimei.util.Switcher;

public class ParameterService {
    private static Logger log = Logger.getLogger(ParameterService.class);

    @Autowired
    private ParameterDAO parameterDAO;

    @Autowired
    private SolrService solrService;

    @Autowired
    private Switcher switcher;

    private AtomicBoolean running = new AtomicBoolean(false);

    private static volatile AtomicInteger id = new AtomicInteger(0);

    private final long maxExpire = 1000 * 60 * 60;

    void init() {

        if (!switcher.isOpen())
            return;

        if (running.get())
            return;
        long start = System.currentTimeMillis();
        try {
            running.set(true);
            log.info("start crawl zol");
            Integer maxId = parameterDAO.getMaxId();
            maxId = (null == maxId) ? 0 : maxId;
            id.addAndGet(maxId);
            String url = "http://wap.zol.com.cn/0/%s/index.html";
            log.info("crawl zol maxid " + maxId);
            while (true) {
                long expire = System.currentTimeMillis() - start;
                if (expire >= maxExpire)
                    return;
                log.info("crawl zol " + maxId);
                try {
                    String html = HttpUtil.get(String.format(url, maxId));
                    if (null == html)
                        continue;
                    html = html.replaceAll("(\\t|\\r|\\n)", "");
                    Pattern pattern = Pattern
                            .compile(".*</a>&gt;(.*)&gt;综述<br/></p><p><img src=\"(.*)\" alt=\".*\" /><br />.*参数</a>】<br />(.*)<br />【<a href=.*");
                    Matcher matcher = pattern.matcher(html);
                    String title = null;
                    String pic = null;
                    String parameters = null;
                    while (matcher.find()) {
                        title = matcher.group(1);
                        pic = matcher.group(2);
                        parameters = matcher.group(3);
                        break;
                    }
                    if (null == title || title.trim().length() == 0
                            || title.equals(""))
                        continue;
                    parameterDAO.insert(maxId, title, parameters, pic);
                    solrService
                            .doParametersIndex(maxId, title, parameters, pic);
                    log.info("crawl zol succ :" + maxId + " " + title + "  "
                            + parameters + "  " + pic);
                } catch (Exception e) {
                    log.error("crawl zol error", e);
                } finally {
                    maxId++;
                    if (System.currentTimeMillis() - start > 30 * 60 * 1000)
                        return;
                }
            }
        } catch (Exception e) {
            log.error("crawl zol error", e);
        } finally {
            running.set(false);
        }
    }

    public List<Parameters> get(String content) {
        content = content.replaceAll(
                MessageTypeEnum.PARAMETER.getReplaceRegx(), "");
        List<Parameters> list = solrService.searchParameters(content);
        if (null == list || list.size() == 0)
            return null;
        if (list.size() > 3)
            return list.subList(0, 3);
        else
            return list;
    }
}
